package com.rdxy.mult.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rdxy.common.utils.VodTemplate;
import com.rdxy.common.vo.Result;
import com.rdxy.mult.entity.Movie;
import com.rdxy.mult.service.IMovieService;
import com.rdxy.sys.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 人道逍遥
 * @since 2023-06-02
 */
@RestController
@RequestMapping("/movie")
public class MovieController {

    @Autowired
    private IMovieService movieService;

    @Autowired
    private VodTemplate vodTemplate;

    @GetMapping("/all")
    public Result<List<Map<String,Object>>> getMovieAll() throws Exception {
        List<Movie> list = movieService.list();
        List<Map<String,Object>> data=new ArrayList<>();

        for (Movie movie : list) {
            Map<String,Object> map=new HashMap<>();
            map.put("cover",movie.getImage());
            map.put("name",movie.getName());
            map.put("vid",movie.getPlayId());

            String playAuth = vodTemplate.getVideoPlayAuth(movie.getPlayId()).getPlayAuth();
            movie.setPlayAuth(playAuth);
            map.put("playauth",movie.getPlayAuth());
            data.add(map);
        }

        return Result.success(data);

    }

    @GetMapping("/list")
    public Result<Map<String,Object>> getMovieList(@RequestParam(value = "name",required = false) String name,
                                                   @RequestParam(value = "cid",required = false) String cid,
                                                   @RequestParam(value = "pageNo") Long pageNo,
                                                   @RequestParam(value = "pageSize") Long pageSize){

        LambdaQueryWrapper<Movie> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(name),Movie::getName,name);
        wrapper.eq(StringUtils.hasLength(cid),Movie::getCid,cid);
        wrapper.orderByDesc(Movie::getId);

        Page<Movie> page = new Page<>(pageNo, pageSize);

        movieService.page(page, wrapper);

        Map<String,Object> data=new HashMap<>();
        data.put("total",page.getTotal());
        data.put("row",page.getRecords());

        return Result.success(data);

    }

    @PostMapping
    public Result<?> addMovie(@RequestBody Movie movie){
        System.out.println("-------------------------------------------");
        System.out.println(movie);

        movieService.save(movie);
        return Result.success("新增用户成功");
    }

    @PutMapping
    public Result<?> updateMovie(@RequestBody Movie movie){
        LambdaQueryWrapper<Movie> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Movie::getId,movie.getId());

        movieService.updateById(movie);
        return Result.success("修改用户成功");
    }

    @GetMapping("/{id}/{is}")
    public Result<Movie> getMovieById(@PathVariable("id") Integer id,@PathVariable(value = "is", required = false) String is) throws Exception {

        //1. 从数据库查询影视信息, 主要字段是playId和image
        Movie movie = movieService.getById(id);
        //2. 根据playId去阿里云换取auth
        if(movie.getPlayId()!=null&&is!=null&&is.equals("1")){
            String playAuth = vodTemplate.getVideoPlayAuth(movie.getPlayId()).getPlayAuth();
            movie.setPlayAuth(playAuth);
            System.out.println(playAuth);
        }

        //3.返回
        return Result.success(movie);
    }




    @DeleteMapping("/{id}")
    public Result<Movie> deleteMovieById(@PathVariable("id") Integer id){
        movieService.removeById(id);

        return Result.success("删除用户成功");
    }

}
